;;; packages.el --- ctrl-ui layer packages file for Spacemacs.
;;

(defconst ctrl-ui-packages
  '(
    (ctrl-mode-line :location built-in)
    )
  )

(defun ctrl-ui/init-ctrl-mode-line ()
  (defun ctrl-ui/display-mode-indent-width ()
    (let ((mode-indent-level
           (catch 'break
             (dolist (test spacemacs--indent-variable-alist)
               (let ((mode (car test))
                     (val (cdr test)))
                 (when (or (and (symbolp mode) (derived-mode-p mode))
                           (and (listp mode) (apply 'derived-mode-p mode))
                           (eq 't mode))
                   (when (not (listp val))
                     (setq val (list val)))
                   (dolist (v val)
                     (cond
                      ((integerp v) (throw 'break v))
                      ((and (symbolp v) (boundp v))
                       (throw 'break (symbol-value v))))))))
             (throw 'break (default-value 'evil-shift-width)))))
      (concat "TS:" (int-to-string (or mode-indent-level 0)))))

  (setq-default mode-line-misc-info
                (assq-delete-all 'which-function-mode mode-line-misc-info))

  (setq-default mode-line-format
                (list
                 " %1"
                 '(:eval (propertize
                          (window-number-mode-line)
                          'face
                          'font-lock-type-face))
                 " "

                 '(:eval (ctrl-ui/update-persp-name))

                 ;; the buffer name; the file name as a tool tip
                 '(:eval (propertize "%b " 'face 'font-lock-keyword-face
                                     'help-echo (buffer-file-name)))

                 " " ;; relative position, size of file
                 (propertize "%o" 'face 'font-lock-constant-face) ;; % above top

                 "/" ;; size
                 (propertize "%I" 'face 'font-lock-constant-face)
                 " "

                 ;; evil state
                 '(:eval evil-mode-line-tag)

                 " " ;; insert vs overwrite mode, input-method in a tooltip
                 '(:eval (propertize (if overwrite-mode "O" "I")
                                     'face 'font-lock-preprocessor-face
                                     'help-echo (concat "Buffer is in "
                                                        (if overwrite-mode
                                                            "overwrite"
                                                          "insert") " mode")))

                 ;; was this buffer modified since the last save?
                 '(:eval (when (buffer-modified-p)
                           (concat "." (propertize "M"
                                                   'face 'font-lock-warning-face
                                                   'help-echo "Buffer has been modified"))))

                 ;; is this buffer read-only?
                 '(:eval (when buffer-read-only
                           (concat "." (propertize "R"
                                                   'face 'font-lock-type-face
                                                   'help-echo "Buffer is read-only"))))
                 "  "

                 ;; the current major mode for the buffer.
                 '(:eval (propertize "%m" 'face 'font-lock-string-face
                                     'help-echo buffer-file-coding-system))

                 ;;  minor modes
                 '(:eval (when (> (window-width) 90)
                           minor-mode-alist))

                 " "
                 ;; git info
                 '(:eval (when (> (window-width) 120)
                           `(vc-mode vc-mode)))
                 " "

                 ;; global-mode-string goes in mode-line-misc-info
                 '(:eval (when (> (window-width) 120)
                           mode-line-misc-info))

                 (mode-line-fill 'mode-line 15)

                 ;; '(:eval (ctrl-ui/display-mode-indent-width))
                 ;; " "

                 " "
                 '(:eval (when (> (window-width) 120)
                           (buffer-encoding-abbrev)))
                 " "

                 ;; line and column
                 " " ;; '%02' to set to 2 chars at least; prevents flickering
                 (propertize "%02l" 'face 'font-lock-type-face) "."
                 (propertize "%02C" 'face 'font-lock-type-face)
                 mode-line-end-spaces
                 )))
